home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / share / hplip / copier / copier.pyc (.txt) < prev   
Encoding:
Python Compiled Bytecode  |  2009-04-20  |  8.7 KB  |  282 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. from __future__ import generators
  5. import sys
  6. import os
  7. import os.path as os
  8. import time
  9. import threading
  10. import Queue
  11. from cStringIO import StringIO
  12. from base.g import *
  13. from base.codes import *
  14. from base import device, utils, status, pml
  15. COPY_CANCELED = 1
  16. STATUS_IDLE = 0
  17. STATUS_SETTING_UP = 1
  18. STATUS_WARMING_UP = 2
  19. STATUS_ACTIVE = 3
  20. STATUS_DONE = 4
  21. STATUS_ERROR = 5
  22.  
  23. class PMLCopyDevice(device.Device):
  24.     
  25.     def __init__(self, device_uri = None, printer_name = None, service = None, callback = None):
  26.         device.Device.__init__(self, device_uri, printer_name, service, callback)
  27.         self.copy_thread = None
  28.  
  29.     
  30.     def copy(self, num_copies = 1, contrast = 0, reduction = 100, quality = pml.COPIER_QUALITY_NORMAL, fit_to_page = pml.COPIER_FIT_TO_PAGE_ENABLED, scan_style = SCAN_STYLE_FLATBED, update_queue = None, event_queue = None):
  31.         if not self.isCopyActive():
  32.             self.copy_thread = PMLCopyThread(self, num_copies, contrast, reduction, quality, fit_to_page, scan_style, update_queue, event_queue)
  33.             self.copy_thread.start()
  34.             return True
  35.         return False
  36.  
  37.     
  38.     def isCopyActive(self):
  39.         if self.copy_thread is not None:
  40.             return self.copy_thread.isAlive()
  41.         return False
  42.  
  43.     
  44.     def waitForCopyThread(self):
  45.         if self.copy_thread is not None and self.copy_thread.isAlive():
  46.             self.copy_thread.join()
  47.         
  48.  
  49.  
  50.  
  51. class PMLCopyThread(threading.Thread):
  52.     
  53.     def __init__(self, dev, num_copies, contrast, reduction, quality, fit_to_page, scan_style, update_queue = None, event_queue = None):
  54.         threading.Thread.__init__(self)
  55.         self.dev = dev
  56.         self.num_copies = num_copies
  57.         self.contrast = contrast
  58.         self.reduction = reduction
  59.         self.quality = quality
  60.         self.fit_to_page = fit_to_page
  61.         self.scan_style = scan_style
  62.         self.event_queue = event_queue
  63.         self.update_queue = update_queue
  64.         self.prev_update = ''
  65.         self.copy_type = self.dev.copy_type
  66.         log.debug('Copy-type = %d' % self.copy_type)
  67.  
  68.     
  69.     def run(self):
  70.         STATE_DONE = 0
  71.         STATE_ERROR = 5
  72.         STATE_ABORTED = 10
  73.         STATE_SUCCESS = 20
  74.         STATE_BUSY = 25
  75.         STATE_SET_TOKEN = 30
  76.         STATE_SETUP_STATE = 40
  77.         STATE_SETUP_PARAMS = 50
  78.         STATE_START = 60
  79.         STATE_ACTIVE = 70
  80.         STATE_RESET_TOKEN = 80
  81.         state = STATE_SETUP_STATE
  82.         while state != STATE_DONE:
  83.             if state == STATE_ABORTED:
  84.                 log.debug('%s State: Aborted' % '********************')
  85.                 self.write_queue(STATUS_DONE)
  86.                 state = STATE_RESET_TOKEN
  87.             
  88.             if state == STATE_ERROR:
  89.                 log.debug('%s State: Error' % '********************')
  90.                 self.write_queue(STATUS_ERROR)
  91.                 state = STATE_RESET_TOKEN
  92.                 continue
  93.             if state == STATE_SUCCESS:
  94.                 log.debug('%s State: Success' % '********************')
  95.                 self.write_queue(STATUS_DONE)
  96.                 state = STATE_RESET_TOKEN
  97.                 continue
  98.             if state == STATE_BUSY:
  99.                 log.debug('%s State: Busy' % '********************')
  100.                 self.write_queue(STATUS_ERROR)
  101.                 state = STATE_RESET_TOKEN
  102.                 continue
  103.             if state == STATE_SET_TOKEN:
  104.                 log.debug('%s State: Acquire copy token' % '********************')
  105.                 self.write_queue(STATUS_SETTING_UP)
  106.                 
  107.                 try:
  108.                     (result_code, token) = self.dev.getPML(pml.OID_COPIER_TOKEN)
  109.                 except Error:
  110.                     log.debug('Unable to acquire copy token (1).')
  111.                     state = STATE_SETUP_STATE
  112.  
  113.             None if result_code > pml.ERROR_MAX_OK else check_token == token
  114.             if state == STATE_SETUP_STATE:
  115.                 log.debug('%s State: Setup state' % '********************')
  116.                 if self.copy_type == COPY_TYPE_DEVICE:
  117.                     (result_code, copy_state) = self.dev.getPML(pml.OID_COPIER_JOB)
  118.                     if copy_state == pml.COPIER_JOB_IDLE:
  119.                         self.dev.setPML(pml.OID_COPIER_JOB, pml.COPIER_JOB_SETUP)
  120.                         state = STATE_SETUP_PARAMS
  121.                     else:
  122.                         state = STATE_BUSY
  123.                 elif self.copy_type == COPY_TYPE_AIO_DEVICE:
  124.                     (result_code, copy_state) = self.dev.getPML(pml.OID_SCAN_TO_PRINTER)
  125.                     if copy_state == pml.SCAN_TO_PRINTER_IDLE:
  126.                         state = STATE_SETUP_PARAMS
  127.                     else:
  128.                         state = STATE_BUSY
  129.                 
  130.             self.copy_type == COPY_TYPE_DEVICE
  131.             if state == STATE_SETUP_PARAMS:
  132.                 log.debug('%s State: Setup Params' % '********************')
  133.                 if self.num_copies < 0:
  134.                     self.num_copies = 1
  135.                 
  136.                 if self.num_copies > 99:
  137.                     self.num_copies = 99
  138.                 
  139.                 if self.copy_type == COPY_TYPE_DEVICE:
  140.                     self.dev.setPML(pml.OID_COPIER_JOB_NUM_COPIES, self.num_copies)
  141.                     self.dev.setPML(pml.OID_COPIER_JOB_CONTRAST, self.contrast)
  142.                     self.dev.setPML(pml.OID_COPIER_JOB_REDUCTION, self.reduction)
  143.                     self.dev.setPML(pml.OID_COPIER_JOB_QUALITY, self.quality)
  144.                     if self.scan_style == SCAN_STYLE_FLATBED:
  145.                         self.dev.setPML(pml.OID_COPIER_JOB_FIT_TO_PAGE, self.fit_to_page)
  146.                     
  147.                 else:
  148.                     self.dev.setPML(pml.OID_COPIER_NUM_COPIES_AIO, self.num_copies)
  149.                     self.contrast = self.contrast * 10 / 25 + 50
  150.                     self.dev.setPML(pml.OID_COPIER_CONTRAST_AIO, self.contrast)
  151.                     if self.fit_to_page == pml.COPIER_FIT_TO_PAGE_ENABLED:
  152.                         self.reduction = 0
  153.                     
  154.                     self.dev.setPML(pml.OID_COPIER_REDUCTION_AIO, self.reduction)
  155.                     self.dev.setPML(pml.OID_COPIER_QUALITY_AIO, self.quality)
  156.                     self.dev.setPML(pml.OID_PIXEL_DATA_TYPE, pml.PIXEL_DATA_TYPE_COLOR_24_BIT)
  157.                     self.dev.setPML(pml.OID_COPIER_SPECIAL_FEATURES, pml.COPY_FEATURE_NONE)
  158.                     self.dev.setPML(pml.OID_COPIER_PHOTO_MODE, pml.ENHANCE_LIGHT_COLORS | pml.ENHANCE_TEXT)
  159.                     self.dev.setPML(pml.OID_COPIER_JOB_INPUT_TRAY_SELECT, pml.COPIER_JOB_INPUT_TRAY_1)
  160.                     self.dev.setPML(pml.OID_COPIER_MEDIA_TYPE, pml.COPIER_MEDIA_TYPE_AUTOMATIC)
  161.                     self.dev.setPML(pml.OID_PIXEL_DATA_TYPE, pml.PIXEL_DATA_TYPE_COLOR_24_BIT)
  162.                     self.dev.setPML(pml.OID_COPIER_SPECIAL_FEATURES, pml.COPY_FEATURE_NONE)
  163.                     self.dev.setPML(pml.OID_COPIER_JOB_MEDIA_SIZE, pml.COPIER_JOB_MEDIA_SIZE_US_LETTER)
  164.                 log.debug('num_copies = %d' % self.num_copies)
  165.                 log.debug('contrast= %d' % self.contrast)
  166.                 log.debug('reduction = %d' % self.reduction)
  167.                 log.debug('quality = %d' % self.quality)
  168.                 log.debug('fit_to_page = %d' % self.fit_to_page)
  169.                 state = STATE_START
  170.                 continue
  171.             if state == STATE_START:
  172.                 log.debug('%s State: Start' % '********************')
  173.                 if self.copy_type == COPY_TYPE_DEVICE:
  174.                     self.dev.setPML(pml.OID_COPIER_JOB, pml.COPIER_JOB_START)
  175.                 elif self.copy_type == COPY_TYPE_AIO_DEVICE:
  176.                     self.dev.setPML(pml.OID_SCAN_TO_PRINTER, pml.SCAN_TO_PRINTER_START)
  177.                 
  178.                 state = STATE_ACTIVE
  179.                 continue
  180.             if state == STATE_ACTIVE:
  181.                 log.debug('%s State: Active' % '********************')
  182.                 if self.copy_type == COPY_TYPE_DEVICE:
  183.                     while True:
  184.                         (result_code, copy_state) = self.dev.getPML(pml.OID_COPIER_JOB)
  185.                         if self.check_for_cancel():
  186.                             self.dev.setPML(pml.OID_COPIER_JOB, pml.COPIER_JOB_IDLE)
  187.                             state = STATE_ABORTED
  188.                             break
  189.                         
  190.                         if copy_state == pml.COPIER_JOB_START:
  191.                             log.debug('state = start')
  192.                             time.sleep(1)
  193.                             continue
  194.                         
  195.                         if copy_state == pml.COPIER_JOB_ACTIVE:
  196.                             self.write_queue(STATUS_ACTIVE)
  197.                             log.debug('state = active')
  198.                             time.sleep(2)
  199.                             continue
  200.                             continue
  201.                         if copy_state == pml.COPIER_JOB_ABORTING:
  202.                             log.debug('state = aborting')
  203.                             state = STATE_ABORTED
  204.                             break
  205.                             continue
  206.                         if copy_state == pml.COPIER_JOB_IDLE:
  207.                             log.debug('state = idle')
  208.                             state = STATE_SUCCESS
  209.                             break
  210.                             continue
  211.                 elif self.copy_type == COPY_TYPE_AIO_DEVICE:
  212.                     while True:
  213.                         (result_code, copy_state) = self.dev.getPML(pml.OID_SCAN_TO_PRINTER)
  214.                         if self.check_for_cancel():
  215.                             self.dev.setPML(pml.OID_SCAN_TO_PRINTER, pml.SCAN_TO_PRINTER_IDLE)
  216.                             state = STATE_ABORTED
  217.                             break
  218.                         
  219.                         if copy_state == pml.SCAN_TO_PRINTER_START:
  220.                             log.debug('state = start')
  221.                             time.sleep(1)
  222.                             continue
  223.                         
  224.                         if copy_state == pml.SCAN_TO_PRINTER_ACTIVE:
  225.                             self.write_queue(STATUS_ACTIVE)
  226.                             log.debug('state = active')
  227.                             time.sleep(2)
  228.                             continue
  229.                             continue
  230.                         if copy_state == pml.SCAN_TO_PRINTER_ABORTED:
  231.                             log.debug('state = aborting')
  232.                             state = STATE_ABORTED
  233.                             break
  234.                             continue
  235.                         if copy_state == pml.SCAN_TO_PRINTER_IDLE:
  236.                             log.debug('state = idle')
  237.                             state = STATE_SUCCESS
  238.                             break
  239.                             continue
  240.                 
  241.             self.copy_type == COPY_TYPE_DEVICE
  242.             if state == STATE_RESET_TOKEN:
  243.                 log.debug('%s State: Release copy token' % '********************')
  244.                 
  245.                 try:
  246.                     self.dev.setPML(pml.OID_COPIER_TOKEN, '\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')
  247.                 except Error:
  248.                     log.error('Unable to release copier token.')
  249.  
  250.                 self.dev.close()
  251.                 state = STATE_DONE
  252.                 continue
  253.  
  254.     
  255.     def check_for_cancel(self):
  256.         canceled = False
  257.         while self.event_queue.qsize():
  258.             
  259.             try:
  260.                 event = self.event_queue.get(0)
  261.                 if event == COPY_CANCELED:
  262.                     canceled = True
  263.                     log.debug('Cancel pressed!')
  264.             continue
  265.             except Queue.Empty:
  266.                 break
  267.                 continue
  268.             
  269.  
  270.             None<EXCEPTION MATCH>Queue.Empty
  271.         return canceled
  272.  
  273.     
  274.     def write_queue(self, message):
  275.         if self.update_queue is not None and message != self.prev_update:
  276.             self.update_queue.put(message)
  277.             time.sleep(0)
  278.             self.prev_update = message
  279.         
  280.  
  281.  
  282.